﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;

namespace LuxsanTool
{
    public delegate void LuxsanStationWorkDelegate(string _strMsgType,string _strLogName,string _strStationName,string _strBackMsg);
    public abstract  class StationWork
    {

        ///<summary>
        /// MES工具配置
        /// </summary>
        public Dictionary<string, LuxsanTool.Tool.BaseTool> m_mesBaseTool=new Dictionary<string, LuxsanTool.Tool.BaseTool> ();

        ///<summary>
        /// MES工具配置
        /// </summary>
        public Dictionary<string, LuxsanTool.Tool.ScriptFactory> m_ScriptFactory = new Dictionary<string, LuxsanTool.Tool.ScriptFactory>();


        /// <summary>
        /// 工站委托 主要显示流程时间 运行步序
        /// </summary>
        public LuxsanStationWorkDelegate luxsanStationWorkDelegate = null;
        /// <summary>
        /// 运行线程
        /// </summary>
        public Thread threadWork;

        /// <summary>
        /// 线程循环
        /// </summary>
        public bool bThreadWork;

        /// <summary>
        /// 单次运行
        /// </summary>
        public bool bSingleRun;

        /// <summary>
        /// 文件路径
        /// </summary>
        public static string m_strFilePath = Application.StartupPath;

        /// <summary>
        /// 保存文件
        /// </summary>
        public static LuxsanBaseHelpLib.FileOperate.IniFile m_iniFile = new LuxsanBaseHelpLib.FileOperate.IniFile();

        public string m_strLogName = "Defealt";
        /// <summary>
        /// 工站名
        /// </summary>
        public  string strStationName;

        /// <summary>
        /// 工站使能
        /// </summary>
        public bool bEnable;

        ///// <summary>
        ///// 异步使能
        ///// </summary>
        //public bool bAsyEnable;
        /// <summary>
        /// 暂停
        /// </summary>
        public  bool bPause = true;

        /// <summary>
        /// 开始
        /// </summary>
        public  bool bStart = false;
        /// <summary>
        /// LOG总使能
        /// </summary>
        public bool bLogEnable;
        /// <summary>
        /// 流程计时
        /// </summary>
        public Stopwatch stopwatch = new Stopwatch();


        public string strCurToolName = "null";

        /// <summary>
        /// 初始化
        /// </summary>
        public abstract void Initial();


        /// <summary>
        /// 释放
        /// </summary>
        public abstract void Realse();


        public void SetFilePath(string _strFilePath,string _strLogPath)
        {
            m_strFilePath = _strFilePath ;
            m_LogFile.SetFilePath(_strLogPath);
        }

        //public void SaveMesTool()
        //{
        //    List<string > listKey = m_mesBsaeTool.Keys.ToList();
        //    List<LuxsanTool.MESTool.MESBaseTool> listValue = m_mesBsaeTool.Values.ToList();

        //    for (int j = 0; j < listValue.Count; j++)
        //    {
        //        //listValue[j].();
        //    }
        //}


        //public abstract void LoadMesTool();


        /// <summary>
        /// 单工站的运行记录
        /// </summary>
        public LuxsanTool.UserControl_Log m_UserControl_Log = new UserControl_Log("工站的运行记录"); 

        /// <summary>
        /// Log
        /// </summary>
        public LuxsanProjectHelpLib.Log.LogFile m_LogFile = new LuxsanProjectHelpLib.Log.LogFile();


        public bool m_bInitial;

        /// <summary>
        /// 添加工具(抽象类内实现)
        /// </summary>
        /// <param name="_strMesToolName"></param>
        /// <param name="_mesBsaeTool"></param>
        public void AbstractAddTool(string _strMesToolName,LuxsanTool.Tool.BaseTool _mesBsaeTool)
        {
            if(!m_mesBaseTool.Keys.Contains(_strMesToolName))
                m_mesBaseTool.Add(_strMesToolName,_mesBsaeTool);
        }

        /// <summary>
        /// 删除工具(抽象类内实现)
        /// </summary>
        /// <param name="_strMesToolName"></param>
        public void AbstractDeleteTool(string _strMesToolName)
        {
            if (m_mesBaseTool.Keys.Contains(_strMesToolName))
                m_mesBaseTool.Remove(_strMesToolName);
        }

        /// <summary>
        /// 添加运行记录
        /// </summary>
        /// <param name="_strStationId"></param>
        /// <param name="_strRunMsg"></param>
        public abstract void AddLog(string _strLogName, string _strRunMsg, bool _bDisForm = false);

        /// <summary>
        /// 添加报警记录
        /// </summary>
        /// <param name="_iErrorCode"></param>
        /// <param name="_strStationId"></param>
        /// <param name="_strRunMsg"></param>
        public abstract void Alarm(int _iErrorCode, string _strLogName, string _strRunMsg, bool _bDisForm = false);


        /// <summary>
        /// 添加运行记录
        /// </summary>
        /// <param name="_strStationId"></param>
        /// <param name="_strRunMsg"></param>
       // public abstract void LogToForm(string _strLogName, string _strRunMsg, bool _bDisForm = false);

        /// <summary>
        /// 运行流程
        /// </summary>
        public abstract void StationProcess();

    }
}


